<?php

namespace app\common\model;

use think\Model;

/**
 * 配置模型
 */
class Chat extends Model
{

    // 表名,不含前缀
    protected $name = 'chat';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    // 追加属性
    protected $append = [
    ];

    /**
     * 关联获取用户信息
     */
    public function user(){
        return $this->belongsTo('User','reply_user_id','id',[],'LEFT')->setEagerlyType(0);
    }

    /**
     * 关联获聊天详情信息
     */
    public function chatdetail(){
        return $this->hasOne('ChatDetail','chat_id');
    }

    public function getCreatetimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }


    public function getList($where,$page){
        try{
            $data=$this
                ->with(['user'])
                ->where($where)
                ->order('createtime desc')
                ->page($page)
                ->limit(10)
                ->select();
            foreach ($data as $row) {
                $row->visible(['id','user_id','reply_user_id','something_id','createtime']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['nickname','avatar']);

            }
            if(!empty($data)){
                $data = collection($data)->toArray();
                foreach ($data as $key=>$val){
                    $ChatDetailModel=new ChatDetail();

                    $charDetail=$ChatDetailModel
                        ->where(['chat_id'=>array('eq',$val['id'])])
                        ->order('createtime desc')
                        ->find();
                    if($val['type']==1){
                        $someThing=Something::get($val['something_id']);
                        if($val['reply_user_id']=$someThing['publisherUserId']){
                            $data[$key]['name']=$someThing->contacts;
                        }elseif($val['user_id']=$someThing['publisherUserId']){
                            $data[$key]['name']=$someThing->contacts;
                        }else{
                            $data[$key]['name']=$val['user']['nickname'];
                        }

                        $data[$key]['content']=$charDetail['content'];
                        $data[$key]['url']=url('index/messageroom',
                            array(
                                'user_id'=>$val['user_id'],
                                'reply_user_id'=>$val['reply_user_id'],
                                'something_id'=>$val['something_id'],
                            )
                        );
                    }elseif ($val['type']==2){
                        $someThing=\app\common\model\AuctionGood::get($val['something_id']);
                        if($val['reply_user_id']=$someThing['publisherUserId']){
                            $data[$key]['name']=$someThing->contacts;
                        }elseif($val['user_id']=$someThing['publisherUserId']){
                            $data[$key]['name']=$someThing->contacts;
                        }else{
                            $data[$key]['name']=$val['user']['nickname'];
                        }

                        $data[$key]['content']=$charDetail['content'];
                        $data[$key]['url']=url('index/messageroom',
                            array(
                                'user_id'=>$val['user_id'],
                                'reply_user_id'=>$val['reply_user_id'],
                                'something_id'=>$val['something_id'],
                            )
                        );
                    }

                }
            }else{
                $data =[];
            }

        }catch (\Exception $e){
            return [];
        }

        return $data;

    }




}
